﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DAL;
using Model;
namespace UI.system
{
    public partial class make : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            MakePage("/index.aspx", "/index.html");
            if (Request["flag"] == "all")
            {
                List<NewsInfo> listni = NewsDAL.FindAll();
                int count = listni.Count();
                int pagesize = (count - 1) / 20 + 1;
                for (int i = 0; i < listni.Count; i++)
                {
                    MakePage("/newsinfo.aspx?id=" + listni[i].id, "/news/newsinfo-" + listni[i].id + ".html");
                }
                for (int i = 0; i < pagesize; i++)
                {
                    MakePage("/news.aspx?pageindex=" + (i + 1), "/news/news-1-" + (i + 1) + ".html");
                }
            }
            Response.Write("<script>parent.$('.btn-success').click();</script>");
        }
        public bool MakePage(string strPageUrl, string strFileName)
        {
            string strDir, strFilePage;
            strDir = @"/";//更新到的文件夹
            strFilePage = Server.MapPath(strDir + strFileName);
            StreamWriter sw = null;
            //获得aspx的静态html
            try
            {
                if (!Directory.Exists(Server.MapPath(strDir)))
                {
                    Directory.CreateDirectory(Server.MapPath(strDir));
                }
                if (File.Exists(strFilePage))
                {
                    File.Delete(strFilePage);
                }
                sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("utf-8"));
                Server.Execute(strPageUrl, sw);
            }
            catch (Exception ex)
            {
                return false;//生成到出错
            }
            finally
            {
                sw.Flush();
                sw.Close();
                sw = null;
            }

            return true;
        }
       
    }
}